package com.file.system.plugin.websocket;

import com.file.system.plugin.shiro.ShiroTools;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;

@Configuration
public class WebSocketConfig extends ServerEndpointConfig.Configurator {

    public static final String SHIRO_ID = "shiroId";
    public static final String USER = "user";

    //这个bean的注册,用于扫描带有@ServerEndpoint的注解成为websocket ,如果你使用外置的tomcat就不需要该配置文件
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

    @Override
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
        // 将用户信息存储到socket的配置里
        sec.getUserProperties().put(USER, ShiroTools.getShiroUser());
        sec.getUserProperties().put(SHIRO_ID, ShiroTools.getSession().getId().toString());
        super.modifyHandshake(sec, request, response);
    }
}
